package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    @Reference
    private IUserInfoCacheService userInfoCacheService;

    public boolean checkPhone(String phone) {
        return userInfoMapper.selectByPhone(phone) == null;
    }

    public AjaxResult userRegist(String phone, String nickname, String verifyCode, String password, String rpassword) {
        AjaxResult result = new AjaxResult();
        try {
            //1.必须先做数据校验
            //非空验证
            Assert.hasLength(verifyCode, "验证码不能为空");
            Assert.hasLength(phone, "手机号不能为空");
            Assert.hasLength(nickname, "昵称不能为空");
            Assert.hasLength(password, "密码不能为空");
            Assert.hasLength(rpassword, "确认不能为空");

            //一致(数据相同)验证
            String cacheVerifyCode = verifyCodeCacheService.getVerifyCode(phone);
            Assert.equalsIgnoreCase(verifyCode, cacheVerifyCode, "验证码输入错误");

            Assert.equalsIgnoreCase(password, rpassword, "两次密码输入不一致");

            //2.封装对象
            UserInfo userInfo = new UserInfo();
            userInfo.setPhone(phone);
            userInfo.setNickname(nickname);
            userInfo.setPassword(password);
            userInfo.setLevel(1);
            userInfo.setCity("地球某个角落");
            // ...

            //3.保存到数据库
            userInfoMapper.insert(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public AjaxResult userLogin(String phone, String password) {
        AjaxResult result = new AjaxResult();
        UserInfo user = userInfoMapper.selectByInfo(phone, password);
        if (user == null) { //登录失败
            return result.mark("账号和密码不匹配");
        }
        //登录成功,生成一个token
        String token = UUID.randomUUID().toString().replace("-", "");
        //缓存对象
        userInfoCacheService.setUserInfo(token, JSON.toJSONString(user));

        //传递token给控制器
        result.setData(token);
        return result;
    }

    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    @Override
    public void update(UserInfo user) {
        userInfoMapper.updata(user);
    }

    @Override
    public UserInfo getUser(Long id) {
        return userInfoMapper.getUser(id);
    }

    @Override
    public void updateHeadImgUrl(String upload,Long id) {
        userInfoMapper.updateHeadImgUrl(upload,id);
    }

    @Override
    public UserInfo get(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public List<UserInfo> listTop10() {
        return userInfoMapper.listTop10();
    }

    @Override
    public List<UserInfo> listByUserIds(List<Long> userIds) {
        return userInfoMapper.listByUserIds(userIds);
    }

    @Override
    public void updateRanking(UserInfo userInfo) {
        userInfoMapper.updateRanking(userInfo);
    }

    @Override
    public void updateUser(UserInfo user) {
        userInfoMapper.updateUser(user);
    }

    @Override//修改用户的手机号
    public void updateUserPhone(Long id, String phone) {
        userInfoMapper.updateUserPhone(id,phone);
    }

    @Override
    public String getUserPassword(Long id) {
        return userInfoMapper.getUserPassword(id);
    }
}
